﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DrivingAlarm.Website
{
    public class HZMotor:WebsiteBase
    {

        private const string WEB_URL = @"http://115.238.28.138:8023/genstudentsimreserve/theMore/jx000{0}";
        private static string[] PLACE_NAME = new string[] { 
            "机场路模拟培训点","艮山东路教学点","城北汽车城点","铁路培训点",
            "保捷点","杭汽校点","下沙经职点","交通职高点"
        };
        private static int[] PLACE_INDEX = new int[]{152,153,154,155,157,158,159,160};
        public HZMotor()
        {
            this.WebsiteName = "杭州机动车驾驶培训公众服务平台";
        }
        public override void WatchOne()
        {
            for (int idx = 0; idx < PLACE_NAME.Length; idx++)
            {
                string strPage = GetPageStr(string.Format(WEB_URL, PLACE_INDEX[idx]));
                if (!AnalysisStr(strPage,idx))
                {
                    MSG_STRUCT msg = new MSG_STRUCT();
                    msg.MsgStr = string.Format("{0}\t不可以预约", PLACE_NAME[idx]);
                    msg.bPlaySound = false;
                    SendMessage(Hwnd_Parent, (int)WM_MSG.WM_MSG_UPDATE_INFO, IntPtr.Zero, ref msg);
                }
            }
        }
        private bool AnalysisStr(string str ,int placeIdx)
        {
            string pattern = @"(?<=td\>)(\d{1,}(?=人)|\d{4}-\d{2}-\d{2}(?=\<))";
            MatchCollection mc = Regex.Matches(str, pattern, RegexOptions.IgnoreCase);
            int idxBase;
            bool bRet = false;
            if (mc.Count>0)
            {
                for (int idx = 0; idx < mc.Count / 3;idx++ )
                {
                    idxBase = idx * 3;
                    DateTime date = Convert.ToDateTime(mc[idxBase].Value);
                    int totalDay = (int)(date - DateTime.Today).TotalDays;
                    if (totalDay>WatchDay)
                    {
                        continue;
                    }
                    if (Convert.ToInt32(mc[idxBase + 1].Value) > Convert.ToInt32(mc[idxBase + 2].Value))
                    {
                        MSG_STRUCT msg = new MSG_STRUCT();
                        msg.MsgStr = string.Format("{0}\t阶段{1}\t可预约\t{2}",
                            PLACE_NAME[placeIdx], idxBase<mc.Count/2?1:2, mc[idxBase]);
                        msg.bPlaySound = true;
                        SendMessage(Hwnd_Parent,(int)WM_MSG.WM_MSG_UPDATE_INFO,IntPtr.Zero,ref msg);
                        bRet = true;
                    }
                }
            }
            return bRet;
        }
    }
}
